package com.apes.auto.car.model;

import com.apes.auto.car.repository.VehicleLabelRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apestech.framework.pinyin.PinyinConverter;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName VehicleLabel
 * @Description TODO
 * @Author pengyb
 * @Date 2019/7/10 18:03
 * @Version 1.0
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VehicleLabel.CONST_TABLE_NAME,indexes = {
        @Index(columnList = "name")
})
public class VehicleLabel extends Model {
    public static final String CONST_TABLE_NAME = "dc_vehicle_label";

    @Id
    @Column(name = "id", length = 30)
    @Comment("厂牌编码")
    private String id;  //编码

    @Column(name = "name", length = 32)
    @Comment("厂牌名称")
    private String name;// 名称


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "carBrand_id", referencedColumnName = "id")
    @Comment("品牌")
    private CarBrand carBrand;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "manufacturers_id", referencedColumnName = "id")
    @Comment("厂商")
    private Manufacturers manufacturers;

    /**
     * 检查厂牌编码
     *
     * @param GB(国别)
     * @param PP(品牌)
     * @param CS(厂商)
     * @return
     */
    public String checkID(String GB, String PP, String CS) throws Exception {
        PinyinConverter pinyinConverter = new PinyinConverter();
        String PPpinyin = pinyinConverter.getPinyin(PP);
        String CSpinyin = pinyinConverter.getPinyin(CS);
        String CP = GB + PPpinyin.substring(0, 1).toUpperCase() + CSpinyin.substring(0, 1).toUpperCase();

        VehicleLabel vehicleLabel = SpringManager.getBean(VehicleLabelRepository.class).findOne(CP);

        if (vehicleLabel == null) { //厂牌可用
            return CP;
        } else { //厂牌不可用
            Map<String, String> map = new HashMap<>();
            //获得所有ID
            List<Map<String, String>> idAll = SpringManager.getBean(VehicleLabelRepository.class).findByAll();
            for (int i = 0; i < PPpinyin.length(); i++) {
                for (int j = 0; j < CSpinyin.length(); j++) {
                    if (i == 0 && j == 0) break;
                    String uniqueCP = GB + String.valueOf(PPpinyin.charAt(i)).toUpperCase() + String.valueOf(CSpinyin.charAt(j)).toUpperCase();
                    map = idAll.stream().filter(o -> o.get("id").equals(uniqueCP)).findAny().orElse(new HashMap<>());
                    if (map.isEmpty()) {
                        return uniqueCP; //厂牌可用 结束循环
                    }
                }
            }
            for (int k = 0; k < idAll.size(); k++) { // 暂定加流水号
                String onlyCP = CP + k;
                map = idAll.stream().filter(o -> o.get("id").equals(onlyCP)).findAny().orElse(new HashMap<>());
                if (map.isEmpty()) {
                    return onlyCP; //厂牌可用 结束循环
                }
            }


        }
        return CP;
    }
}
